﻿Imports Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
Public Class CheckUserName
    Public Shared Sub Checkusername(ByVal connection As WqlConnectionManager, ByVal uname As String, ByVal dom As String,
                                    ByVal fuzzy As Boolean, ByVal rdp As Boolean, ByVal seaType As String)
        Dim resId As String = Nothing
        Dim macAddress As String = Nothing
        ' ReSharper disable RedundantAssignment
        Dim comname As String = Nothing
        Dim serialNum As String = Nothing
        Dim userName As String = Nothing
        Dim consolemin As String = Nothing
        Dim consolecount As String = Nothing
        Dim struname As String = Nothing
        Dim strdom As String = Nothing
        ' ReSharper restore RedundantAssignment
        Dim userlist As List(Of String) = New List(Of String)
        Dim residarr As List(Of String) = New List(Of String)
        Dim strcomsysqry As String
        If SeaType = "Last User Logon" Then
            If fuzzy Then
                strcomsysqry = "select * from SMS_R_SYSTEM where SMS_r_SYSTEM.LastLogonUserName like '%" & uname & "%' and SMS_r_SYSTEM.LastLogonUserDomain like '%" & dom & "%'"
            Else
                strcomsysqry = "select * from SMS_R_SYSTEM where SMS_r_SYSTEM.LastLogonUserName = '" & uname & "' and SMS_r_SYSTEM.LastLogonUserDomain = '" & dom & "'"
            End If
            Dim objcomsys = connection.QueryProcessor.ExecuteQuery(strcomsysqry)
            For Each rescomsys In objcomsys
                resId = rescomsys.Properties("ResourceID").stringvalue().ToString()
                residarr.Add(resId)
                If _
                    Not _
                    userlist.Contains(
                        rescomsys.Properties("LastLogonUserDomain").stringvalue().ToString() & "\" &
                        UCase(rescomsys.Properties("LastLogonUserName").stringvalue().ToString())) Then
                    userlist.Add(
                        rescomsys.Properties("LastLogonUserDomain").stringvalue().ToString() & "\" &
                        UCase(rescomsys.Properties("LastLogonUserName").stringvalue().ToString()))
                End If
            Next
        ElseIf seaType = "Console Count" Then
            Dim objcomsys = connection.QueryProcessor.ExecuteQuery("select * from SMS_g_system_system_console_user")
            For Each rescomsys In objcomsys
                strdom = UCase(Split(rescomsys.Properties("SystemConsoleUser").stringvalue().ToString(), "\")(0).ToString())
                struname = UCase(Split(rescomsys.Properties("SystemConsoleUser").stringvalue().ToString(), "\")(1).ToString())
                If strdom.Contains(UCase(dom)) And struname.Contains(UCase(uname)) And fuzzy Then
                    resId = rescomsys.Properties("ResourceID").stringvalue().ToString()
                    residarr.Add(resId)
                    If _
                        Not _
                        userlist.Contains(
                            rescomsys.Properties("SystemConsoleUser").stringvalue().ToString()) Then
                        userlist.Add(
                            rescomsys.Properties("SystemConsoleUser").stringvalue().ToString())
                    End If
                ElseIf strdom = dom And struname = uname And fuzzy = False Then
                    resId = rescomsys.Properties("ResourceID").stringvalue().ToString()
                    residarr.Add(resId)
                    If _
                        Not _
                        userlist.Contains(
                            rescomsys.Properties("SystemConsoleUser").stringvalue().ToString()) Then
                        userlist.Add(
                            rescomsys.Properties("SystemConsoleUser").stringvalue().ToString())
                    End If
                End If
            Next
        ElseIf seaType = "Primary Device" Then
            Dim objcomsys = connection.QueryProcessor.ExecuteQuery("select * from SMS_UserMachineRelationship")
            For Each rescomsys In objcomsys
                strdom = UCase(Split(rescomsys.Properties("UniqueUserName").stringvalue().ToString(), "\")(0).ToString())
                struname = UCase(Split(rescomsys.Properties("UniqueUserName").stringvalue().ToString(), "\")(1).ToString())
                If strdom.Contains(UCase(dom)) And struname.Contains(UCase(uname)) And fuzzy Then
                    resId = rescomsys.Properties("ResourceID").stringvalue().ToString()
                    residarr.Add(resId)
                    If _
                        Not _
                        userlist.Contains(
                            rescomsys.Properties("UniqueUserName").stringvalue().ToString()) Then
                        userlist.Add(
                            rescomsys.Properties("UniqueUserName").stringvalue().ToString())
                    End If
                ElseIf strdom = dom And struname = uname And fuzzy = False Then
                    resId = rescomsys.Properties("ResourceID").stringvalue().ToString()
                    residarr.Add(resId)
                    If _
                        Not _
                        userlist.Contains(
                            rescomsys.Properties("UniqueUserName").stringvalue().ToString()) Then
                        userlist.Add(
                            rescomsys.Properties("UniqueUserName").stringvalue().ToString())
                    End If
                End If
            Next
        End If
        If userlist.Count = 1 Then
            Dim comdetails = New List(Of String)
            If residarr.Count > 1 Then
                For Each resourceid In residarr
                    Dim objrsys =
                            connection.QueryProcessor.ExecuteQuery(
                                "select * from SMS_R_System join SMS_G_System_PC_BIOS on SMS_R_System.resourceid = SMS_G_System_PC_BIOS.resourceid join SMS_g_system_system_console_user on SMS_g_system_system_console_user.resourceID = SMS_R_System.resourceid where ResourceID = '" & resourceid & "'")
                    For Each res In objrsys
                        Dim smssys As Object
                        smssys = res.properties("SMS_R_System").objectvalue
                        resId = DirectCast(smssys.Properties("ResourceID"), Management.PropertyData).Value
                        userName = DirectCast(smssys.Properties("LastLogonUserDomain"), Management.PropertyData).Value & "\" &
                                   DirectCast(smssys.Properties("LastLogonUserName"), Management.PropertyData).Value
                        comname = DirectCast(smssys.Properties("Name"), Management.PropertyData).Value
                        Dim objbios As Object
                        objbios = res.properties("SMS_G_System_PC_BIOS").objectvalue
                        serialNum = DirectCast(objbios.Properties("SerialNumber"), Management.PropertyData).Value
                        Dim objconsol As Object
                        objconsol = res.properties("SMS_g_system_system_console_user").objectvalue
                        If UCase(userName) = UCase(DirectCast(objconsol.Properties("SystemConsoleUser"), Management.PropertyData).Value) Then
                            consolemin = DirectCast(objconsol.Properties("TotalUserConsoleMinutes"), Management.PropertyData).Value
                            consolecount = DirectCast(objconsol.Properties("NumberOfConsoleLogons"), Management.PropertyData).Value
                        End If
                        If Not macAddress = Nothing Then
                            If macAddress.StartsWith(",") Then
                                Dim mac = macAddress.Split(",")
                                macAddress = mac(1).ToString()
                            End If
                        End If
                        comdetails.Add(userName & "|" & comname & "|" & serialNum & "|" & consolemin & "|" & consolecount & "|" & resId)
                    Next
                Next
                FillForm.FillFormboxes(comdetails, "comp", rdp)
            Else
                GetSMSProvideData.Getcomsys(connection, "ResourceID = '" & resId & "'", rdp)
            End If
        ElseIf userlist.Count > 1 Then
            FillForm.FillFormboxes(userlist, "user", rdp)
        Else
            NewUi.tslbl.Text = "Can't find user " & uname & " in " & dom & " Please try again"
            NewUi.tbseauname.Text = ""
        End If
    End Sub
End Class